﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace SERIX.Report
{
    public class Report
    {
        static public string ImprimirRelatorio(String formato, System.Data.DataSet ds, String Relatorio, bool oneTable)
        {

            String Diretorio = HttpContext.Current.Server.MapPath(Relatorio);
            ReportDocument rpt = new ReportDocument();
            rpt.Load(Diretorio);
            if (oneTable)
            {
                rpt.SetDataSource(ds.Tables[0]);
            }
            else
            {
                rpt.SetDataSource(ds);
            }
            
            DiskFileDestinationOptions File = new DiskFileDestinationOptions();
            string valor = "";
            switch (formato)
            {
                case ".pdf":
                    {
                        String FileName = HttpContext.Current.Server.MapPath("~/Report/Report.pdf");
                        File.DiskFileName = FileName;
                        rpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        rpt.ExportOptions.DestinationOptions = File;
                        rpt.ExportToDisk(ExportFormatType.PortableDocFormat, File.DiskFileName);
                        //ScriptManager.RegisterStartupScript(Page, GetType(), "Key", "javascript:window.open('/Report/Report.pdf', 'SerixWEB','menubar=no, resizable=yes, scrollbars=yes, location=no')", true);
                        valor = "~/Report/Report.pdf";
                        break;
                    }
                case ".doc":
                    {
                        String FileName = HttpContext.Current.Server.MapPath("~/Report/Report.doc");
                        File.DiskFileName = FileName;
                        rpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        rpt.ExportOptions.DestinationOptions = File;
                        rpt.ExportToDisk(ExportFormatType.WordForWindows, File.DiskFileName);
                        valor = "~/Report/Report.doc";
                        
                        break;
                    }
                case ".xls":
                    {
                        String FileName = HttpContext.Current.Server.MapPath("~/Report/Report.xls");
                        File.DiskFileName = FileName;
                        rpt.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        rpt.ExportOptions.DestinationOptions = File;
                        rpt.ExportToDisk(ExportFormatType.Excel, File.DiskFileName);
                        valor = "~/Report/Report.xls";
                        break;
                    }
            }
            return valor;
        }
    }
}
